package com.damai.controller;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import com.citywithincity.models.ModelNotFoundException;
import com.citywithincity.models.Models;
import com.citywithincity.utils.ClassUtil;
import com.damai.action.factories.MchJsonActionFactory;
import com.damai.annotation.Action;
import com.damai.annotation.Controller;
import com.damai.annotation.Crypt;
import com.damai.annotation.Token;
import com.damai.controller.entities.ConfigValue;
import com.damai.error.ApiError;
import com.damai.error.ErrorUtil;
import com.damai.utils.Caster;

@Token
@Controller(key = "config_api")
public class ConfigApi {

	@Crypt(Crypt.BOTH)
	public void setValue(String key,Object value){
		int pos = key.lastIndexOf('.');
		String className = key.substring(0,pos);
		String fieldName = key.substring(pos+1);
		try{
			Class<?> clazz = Class.forName(className);
			ClassUtil.setStaticFieldValue(clazz, fieldName, value);
		}catch(ClassNotFoundException e){
			throw new ApiError(ErrorUtil.printException(e));
		} catch (Exception e) {
			throw new ApiError(ErrorUtil.printException(e));
		}
	}
	
	@Crypt(Crypt.BOTH)
	public Object getValue(String key){
		int pos = key.lastIndexOf('.');
		String className = key.substring(0,pos);
		String fieldName = key.substring(pos+1);
		try{
			Class<?> clazz = Class.forName(className);
			return ClassUtil.getStaticFieldValue(clazz, fieldName);
		}catch(ClassNotFoundException e){
			throw new ApiError(ErrorUtil.printException(e));
		} catch (Exception e) {
			throw new ApiError(ErrorUtil.printException(e));
		}
	}
	
	@Action(MchJsonActionFactory.class)
	public void set(String className,String name,String value){
		try {
			Object model = Models.getBean(className);
			if(model==null){
				model = Models.getModel(Class.forName(className));
			}
			Field field = model.getClass().getDeclaredField(name);
			field.setAccessible(true);
			if(Modifier.isStatic(field.getModifiers())){
				field.set(null, Caster.to(value,field.getType()));
			}else{
				field.set(model, Caster.to(value,field.getType()));
			}
		}catch (ClassNotFoundException e) {
			throw new ApiError("未找到该类");
		} catch (ModelNotFoundException e) {
			throw new ApiError("未找到该模型");
		} catch (NoSuchFieldException e) {
			throw new ApiError(e.getMessage());
		} catch (SecurityException e) {
			throw new ApiError(e.getMessage());
		} catch (IllegalArgumentException e) {
			throw new ApiError(e.getMessage());
		} catch (IllegalAccessException e) {
			throw new ApiError(e.getMessage());
		} 
		
	}
	
	@Action(MchJsonActionFactory.class)
	public List<ConfigValue> query(String className){
		try {
			Object model = Models.getBean(className);
			if(model==null){
				model = Models.getModel(Class.forName(className));
			}
			Field[] fields = model.getClass().getDeclaredFields();
			List<ConfigValue> list = new ArrayList<ConfigValue>(fields.length);
			for (Field field : fields) {
				if(ClassUtil.isSimple(field.getType())){
					field.setAccessible(true);
					//configvalue
					ConfigValue value = new ConfigValue();
					value.setName(field.getName());
					if(Modifier.isStatic(field.getModifiers())){
						value.setValue(String.valueOf(field.get(null)));
					}else{
						value.setValue(String.valueOf(field.get(model)));
					}
					list.add(value);
				}
			}
			return list;
		} catch (ClassNotFoundException e) {
			throw new ApiError("未找到该类");
		} catch (ModelNotFoundException e) {
			throw new ApiError("未找到该模型");
		} catch (IllegalArgumentException e) {
			throw new ApiError(e.getMessage());
		} catch (IllegalAccessException e) {
			throw new ApiError(e.getMessage());
		}
	}
	
}
